package chzhenjia.learn.androidcode.four;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.net.HttpURLConnection;
import java.net.URL;

import chzhenjia.learn.androidcode.R;

public class FourOneActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView ivNetwork;
    private static Handler handler;

    static class LocalHandler extends Handler {
        private FourOneActivity activity;

        public LocalHandler(FourOneActivity activity) {
            this.activity = activity;
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    activity.ivNetwork.setImageBitmap((Bitmap) msg.obj);
                    break;
                case 0:
                    Toast.makeText(activity, "加载图片失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four_one);
        Button btnLoadImage = (Button) findViewById(R.id.btn_load_image);
        btnLoadImage.setOnClickListener(this);
        ivNetwork = (ImageView) findViewById(R.id.iv_network);
        handler = new LocalHandler(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_load_image:
                loadImage();
                break;
        }
    }

    /**
     * 加载网络图片
     */
    private void loadImage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String path = "http://192.168.31.144/test/2.jpg";
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(3000);
                    connection.connect();
                    Message msg = Message.obtain();
                    if (connection.getResponseCode() == 200) {
                        Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());
                        msg.what = 1;
                        msg.obj = bitmap;
//                        ivNetwork.setImageBitmap(bitmap);
                    } else {
                        msg.what = 0;
                    }
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
